<?php

/**
 * ExtServerActionBase class file
 *
 * @author TrueSoftware B.V. (The Netherlands)
 * @link http://www.ext4yii.com
 * @copyright Copyright 2007-2010 TrueSoftwareB.V. all rights reserved.
 * @license http://www.ext4yii.com/license/
 *          EVALUATION VERSION - NOT FOR COMMERCIAL USE OR DISTRIBUTION.
 * @version 0.7.2
 */
abstract class ExtServerActionBase extends ExtActionBase
{

    private $_baseparams_class_type = array('ExtParameter');
    protected $_is_success_function;
    protected $_has_success_handler = false;
    protected $serverparams = array();

    public function parseNode(ExtHTMLParserNode $node, ExtASTWalker $astwalker)
    {
        parent::parseNode($node, $astwalker);
        if (trim($this->getAction()) == '')
            $this->setAction($_GET['r']);

        $handler = $this->parseScriptProperty('oncompletehandler', null, $node, $astwalker);
        if (!is_null($handler))
            $this->setOnCompleteHandler($handler);

        $this->serverparams = $this->parseParameters($node, $astwalker,
                'baseparameters',
                null,
                $this->_baseparams_class_type);
    }

    private function checkSetLoadMask()
    {
        if ($this->getLoadMask())
        {
            if (trim($this->getLoadMaskMsg()) == '')
                $this->setLoadMaskMsg(Ext4Yii::t('ext4yii', 'Please Wait...'));

            $ctl = trim($this->getControlToMask());
            if ($ctl == '' || $ctl == 'this')
                $this->setControlToMask('this.getEl()');
            elseif ($ctl == 'body')
                $this->setControlToMask('Ext.getBody()');
            elseif ($ctl == 'parent')
                $this->setControlToMask('this.ownerCt.getEl()');
            else
                $this->setControlToMask('Ext.getCmp("' . $ctl . '").getEl()');
        }
    }

    protected function checkSetHandler()
    {
        // if we have a handler
        if (trim($this->getOnCompleteHandler()) != '')
        {
            $this->_is_success_function = false;
        }
        else if (trim($this->getOnCompleteFunction()) != '')
        {
            $this->_is_success_function = true;
            $this->setOnCompleteHandler($this->getOnCompleteFunction());
        }
        else
        {
            $this->setOnCompleteHandler('return true;');
        }
    }

    public function createContainer(ExtHTMLParserNode $node)
    {
        $node->shiftUp();
        return null;
    }

    public function renderConfig($asobject=true)
    {
        $this->checkSetHandler();
        $this->checkSetLoadMask();
        $this->createEvent();
        return null;
    }

    private function createEvent()
    {
        $result = "";
        $args = $this->parent->getFunctionArgsByEventName($this->getOnEvent());

        if ($this->getDisableControl())
            $this->parent->setJavaScriptConfig('_ext4yii_disableOnServerAction', true);

        if ($this->_is_success_function)
        {
            $sname = $this->getOnCompleteHandler();
        }
        else
        {
            $sname = $this->parent->getId() . '_ext4yii_' . $this->getOnEvent() . '_action_success';
            $this->parent->setJavaScriptConfig($sname, $this->wrapLooseScript($this->getOnCompleteHandler(), $args));
            $sname = 'this.' . $sname;
        }

        $fname = $this->parent->getId() . '_ext4yii_' . $this->getOnEvent() . '_action_failure';
        $args = $this->parent->getFunctionArgsByEventName($this->getOnEvent(), true);
        $args[] = 'response';
        $args = implode(',', $args);
        $this->parent->setJavaScriptConfig($fname, $this->wrapLooseScript('return true;', $args));
        $fname = 'this.' . $fname;

        if ($this->getLoadMask())
        {
            $maskMsg = new ExtJavaScriptObject();
            $maskMsg->addString('msg', $this->getLoadMaskMsg());

            $mask = new ExtJavaScriptObject();
            $mask->addMember('ctl', $this->getControlToMask());
            $mask->addJavaScriptObject('msg', $maskMsg);
        }
        else
            $mask = null;

        $params = new ExtJavaScriptObject();
        $params->addMember('sender', 'this');
        $params->addMember('sucess', $sname);
        $params->addMember('failure', $fname);
        $params->addArray('args', $this->parent->getFunctionArgsByEventName($this->getOnEvent(), true, true,array('js://response')));
        $params->addJavaScriptObject('mask', $mask);

        $request = new ExtJavaScriptObject();
        $request->addString('url', $this->getAction());
        $request->addString('method', 'post');
        $request->addMember('success', 'ext4yii.defaultOnSucessHandler');
        $request->addMember('failure', 'ext4yii.defaultOnFailureHandler');
        $request->addMember('delay', trim($this->getDelay()) != '' ? $this->getDelay() : 'null');
        $request->addJavaScriptObject('afxparams', $params);
        $this->serverparams['sender'] = 'js://this.id';
        $request->addMember('params', $this->createObjectConfig('parameters',$this->serverparams));

        $result = sprintf('function(%s) { return ext4yii.performServerAction(%s) } ',
                        $args,
                        $request->render());

        $funcname = $this->parent->getId() . '_ext4yii_server_' . $this->getOnEvent();
        $this->parent->setJavaScriptConfig($funcname, $result);
        $this->parent->addEvent($this->getOnEvent(),
                'server',
                $args,
                $funcname);
    }

}

?>
